﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Elentok.Common;
using AMail.Common.Interfaces.Services;
using AMail.Modules.Data;
using Elentok.Common.Async;
using Elentok.Common.Helpers;

namespace AMail.Tool
{
  class Program
  {
    static void WriteUsage()
    {
      Console.WriteLine("AMail Tool");
      Console.WriteLine("------------------------");
      Console.WriteLine("Usage: ");
      Console.WriteLine();
      Console.WriteLine("- To decrypt a file:   AMailTool.exe decrypt {passphrase} {filename} {outputfile}");
      Console.WriteLine("- To encrypt a file:   AMailTool.exe encrypt {passphrase} {filename} {outputfile}");
      Console.WriteLine("- To change the password of an archive:   AMailTool.exe changepass {oldpassphrase} {newpassphrase} {filename}");
    }

    static void Main(string[] args)
    {
      if (args.Length < 4)
      {
        WriteUsage();
        return;
      }

      if (args[0] == "encrypt")
        Encrypt(args[1], args[2], args[3]);
      else if (args[0] == "decrypt")
        Decrypt(args[1], args[2], args[3]);
      else if (args[0] == "changepass")
        ChangePassword(args[1], args[2], args[3]);

    }

    static void Encrypt(string passphrase, string filename, string outputFile)
    {
      MemoryStream encryptedStream;
      using (FileStream plainTextStream = File.OpenRead(filename))
      {
        encryptedStream = (MemoryStream)CryptoHelper.AesEncrypt(plainTextStream, passphrase);
      }

      File.WriteAllBytes(outputFile, encryptedStream.ToArray());
      encryptedStream.Close();
    }

    static void Decrypt(string passphrase, string filename, string outputFile)
    {
      FileStream stream = File.OpenRead(filename);
      using (MemoryStream plainText = (MemoryStream)CryptoHelper.AesDecrypt(stream, passphrase))
      {
        File.WriteAllBytes(outputFile, plainText.ToArray());
      }

      //string data = Encoding.UTF8.GetString(plainText.ToArray());
      //Console.Write(data);
    }

    static void ChangePassword(string oldPassphrase, string newPassphrase, string filename)
    {
      IDataService service = new DataService();
      var archive = service.OpenArchive(filename, oldPassphrase);
      archive.ChangePassword(newPassphrase, new ConsoleProgressNotifier());
    }
  }

  public class ConsoleProgressNotifier : IAsyncProgressNotifier
  {
    #region IProgressNotifier Members

    public void NotifyProgress(int percentage, string text)
    {
    }

    #endregion

    public bool IsPendingCancellation
    {
      get { return false; }
    }

    public void NotifyProgress(int? progress, string message)
    {
      NotifyProgress(progress, message, null);
    }

    public void NotifyProgress(int? progress, string message, object data)
    {
      Console.WriteLine("{0:##0}%: {1}", progress, message);
    }
  }


}
